﻿using System;
namespace ConsoleApp3_c4
{
    class Program
    {
        //定义节点类
        public class Node<T>
        {
            //数据
            public T Data { get; }
            //下一个节点
            public Node<T> Next { get; set; }
            //构造函数

            public Node(T data) => Data = data;
        }

        //定义泛型列表类
        public class GenericList<T>
        {
            private Node<T> head;
            private Node<T> tail;

            //构造函数
            public void Add(T t)
            {
                Node<T> n = new Node<T>(t);
                if (tail == null) head = tail = n;
                else
                {
                    tail.Next = n;
                    tail = n;
                }
            }

            //遍历列表
            public void ForEach(Action<T> action)
            {
                Node<T> current = head;
                while (current != null)
                {
                    action(current.Data);
                    current = current.Next;
                }
            }
        }
        static void Main(string[] args)
        {
            //创建列表
            var list = new GenericList<int>();
            list.Add(2);
            list.Add(5);
            list.Add(8);

            //打印元素
            list.ForEach(x => Console.Write(x + " "));

            //计算统计值
            int max = int.MinValue, min = int.MaxValue, sum = 0;

            //使用ForEach方法遍历列表
            list.ForEach(x =>
            {
                max = Math.Max(max, x);
                min = Math.Min(min, x);
                sum += x;
            });

            Console.WriteLine($"\nMax: {max}, Min: {min}, Sum: {sum}");
        }
    }
}